
#include "HT162X_Parts_Num.h"
#include "HT162X.h"
#include "HT162X_Pin.h"
#include "../MCU_Mold/MCU_AbstractDigitron.h"

const uint8_t TempSet_seg1[8][2] = 
{
    {Cad_Com1,Cad_Pin_13}, //A
    {Cad_Com2,Cad_Pin_13}, //B
    {Cad_Com3,Cad_Pin_13}, //C
    {Cad_Com4,Cad_Pin_13}, //D
    {Cad_Com4,Cad_Pin_14}, //E
    {Cad_Com2,Cad_Pin_14}, //F
    {Cad_Com3,Cad_Pin_14}, //G
    {0,0}, //.
};
const uint8_t TempSet_seg2[8][2] = 
{
    {Cad_Com1,Cad_Pin_11}, //A
    {Cad_Com2,Cad_Pin_11}, //B
    {Cad_Com3,Cad_Pin_11}, //C
    {Cad_Com4,Cad_Pin_11}, //D
    {Cad_Com4,Cad_Pin_12}, //E
    {Cad_Com2,Cad_Pin_12}, //F
    {Cad_Com3,Cad_Pin_12}, //G
    {0,0}, //.
};
const uint8_t TempSet_seg3[8][2] = 
{
    {Cad_Com1,Cad_Pin_9}, //A
    {Cad_Com2,Cad_Pin_9}, //B
    {Cad_Com3,Cad_Pin_9}, //C
    {Cad_Com4,Cad_Pin_9}, //D
    {Cad_Com4,Cad_Pin_10}, //E
    {Cad_Com2,Cad_Pin_10}, //F
    {Cad_Com3,Cad_Pin_10}, //G
    {0,0}, //.
};
const uint8_t TempAct_seg1[8][2] = 
{
    {Cad_Com5,Cad_Pin_13}, //A
    {Cad_Com6,Cad_Pin_13}, //B
    {Cad_Com7,Cad_Pin_13}, //C
    {Cad_Com8,Cad_Pin_13}, //D
    {Cad_Com8,Cad_Pin_14}, //E
    {Cad_Com6,Cad_Pin_14}, //F
    {Cad_Com7,Cad_Pin_14}, //G
    {0,0}, //.
};
const uint8_t TempAct_seg2[8][2] = 
{
    {Cad_Com5,Cad_Pin_11}, //A
    {Cad_Com6,Cad_Pin_11}, //B
    {Cad_Com7,Cad_Pin_11}, //C
    {Cad_Com8,Cad_Pin_11}, //D
    {Cad_Com8,Cad_Pin_12}, //E
    {Cad_Com6,Cad_Pin_12}, //F
    {Cad_Com7,Cad_Pin_12}, //G
    {0,0}, //.
};
const uint8_t TempAct_seg3[8][2] = 
{
    {Cad_Com5,Cad_Pin_9}, //A
    {Cad_Com6,Cad_Pin_9}, //B
    {Cad_Com7,Cad_Pin_9}, //C
    {Cad_Com8,Cad_Pin_9}, //D
    {Cad_Com8,Cad_Pin_10}, //E
    {Cad_Com6,Cad_Pin_10}, //F
    {Cad_Com7,Cad_Pin_10}, //G
    {0,0}, //.
};
const uint8_t Timer_seg1[8][2] = 
{
    {Cad_Com5,Cad_Pin_25}, //A
    {Cad_Com6,Cad_Pin_25}, //B
    {Cad_Com7,Cad_Pin_25}, //C
    {Cad_Com8,Cad_Pin_25}, //D
    {Cad_Com8,Cad_Pin_26}, //E
    {Cad_Com6,Cad_Pin_26}, //F
    {Cad_Com7,Cad_Pin_26}, //G
    {0,0}, //.
};
const uint8_t Timer_seg2[8][2] = 
{
    {Cad_Com5,Cad_Pin_23}, //A
    {Cad_Com6,Cad_Pin_23}, //B
    {Cad_Com7,Cad_Pin_23}, //C
    {Cad_Com8,Cad_Pin_23}, //D
    {Cad_Com8,Cad_Pin_24}, //E
    {Cad_Com6,Cad_Pin_24}, //F
    {Cad_Com7,Cad_Pin_24}, //G
    {0,0}, //.
};
const uint8_t Timer_seg3[8][2] = 
{
    {Cad_Com5,Cad_Pin_21}, //A
    {Cad_Com6,Cad_Pin_21}, //B
    {Cad_Com7,Cad_Pin_21}, //C
    {Cad_Com8,Cad_Pin_21}, //D
    {Cad_Com8,Cad_Pin_22}, //E
    {Cad_Com6,Cad_Pin_22}, //F
    {Cad_Com7,Cad_Pin_22}, //G
    {0,0}, //.
};
const uint8_t Timer_seg4[8][2] = 
{
    {Cad_Com5,Cad_Pin_19}, //A
    {Cad_Com6,Cad_Pin_19}, //B
    {Cad_Com7,Cad_Pin_19}, //C
    {Cad_Com8,Cad_Pin_19}, //D
    {Cad_Com8,Cad_Pin_20}, //E
    {Cad_Com6,Cad_Pin_20}, //F
    {Cad_Com7,Cad_Pin_20}, //G
    {0,0}, //.
};
const uint8_t P1_seg1[8][2] = 
{
    {Cad_Com4,Cad_Pin_25}, //A
    {Cad_Com3,Cad_Pin_25}, //B
    {Cad_Com2,Cad_Pin_25}, //C
    {Cad_Com1,Cad_Pin_25}, //D
    {Cad_Com1,Cad_Pin_26}, //E
    {Cad_Com3,Cad_Pin_26}, //F
    {Cad_Com2,Cad_Pin_26}, //G
    {0,0}, //.
};
const uint8_t P1_seg2[8][2] = 
{
    {Cad_Com4,Cad_Pin_23}, //A
    {Cad_Com3,Cad_Pin_23}, //B
    {Cad_Com2,Cad_Pin_23}, //C
    {Cad_Com1,Cad_Pin_23}, //D
    {Cad_Com1,Cad_Pin_24}, //E
    {Cad_Com3,Cad_Pin_24}, //F
    {Cad_Com2,Cad_Pin_24}, //G
    {0,0}, //.
};
const uint8_t P1_seg3[8][2] = 
{
    {Cad_Com4,Cad_Pin_21}, //A
    {Cad_Com3,Cad_Pin_21}, //B
    {Cad_Com2,Cad_Pin_21}, //C
    {Cad_Com1,Cad_Pin_21}, //D
    {Cad_Com1,Cad_Pin_22}, //E
    {Cad_Com3,Cad_Pin_22}, //F
    {Cad_Com2,Cad_Pin_22}, //G
    {0,0}, //.
};
const uint8_t P2_seg1[8][2] = 
{
    {Cad_Com4,Cad_Pin_19}, //A
    {Cad_Com3,Cad_Pin_19}, //B
    {Cad_Com2,Cad_Pin_19}, //C
    {Cad_Com1,Cad_Pin_19}, //D
    {Cad_Com1,Cad_Pin_20}, //E
    {Cad_Com3,Cad_Pin_20}, //F
    {Cad_Com2,Cad_Pin_20}, //G
    {0,0}, //.
};
const uint8_t P2_seg2[8][2] = 
{
    {Cad_Com4,Cad_Pin_17}, //A
    {Cad_Com3,Cad_Pin_17}, //B
    {Cad_Com2,Cad_Pin_17}, //C
    {Cad_Com1,Cad_Pin_17}, //D
    {Cad_Com1,Cad_Pin_18}, //E
    {Cad_Com3,Cad_Pin_18}, //F
    {Cad_Com2,Cad_Pin_18}, //G
    {0,0}, //.
};
const uint8_t P2_seg3[8][2] = 
{
    {Cad_Com4,Cad_Pin_15}, //A
    {Cad_Com3,Cad_Pin_15}, //B
    {Cad_Com2,Cad_Pin_15}, //C
    {Cad_Com1,Cad_Pin_15}, //D
    {Cad_Com1,Cad_Pin_16}, //E
    {Cad_Com3,Cad_Pin_16}, //F
    {Cad_Com2,Cad_Pin_16}, //G
    {0,0}, //.
};


void Lcd_Number(uint8_t num,uint8_t (*array)[2])
{
    uint8_t i;
    
    for (i = 0; i < 8; i++)
    {
        Cad_Dat.Cad_Com = array[i][0];
        Cad_Dat.Cad_Pin = array[i][1];
        Cad_Dat.Cad_Dis = (((Nixie_NumEncoder[num] >> i) + Nixie_Num_Inverse) & 0x01);
        HT_RAM_Setting(Cad_Dat); 
    }
}
//Numbers
void Lcd_Numbers(int32_t param,uint8_t (*array4)[2],uint8_t (*array3)[2],uint8_t (*array2)[2],uint8_t (*array1)[2])
{
    uint8_t temp;

    if((*array4) != NULL)
    {
        if(param == LcdSeg_Shine_OFF) temp = (LcdSeg_Shine_OFF & 0xf);
        else temp = (param / 1000) % 10;
        Lcd_Number(temp,array4);
    }
    if((*array3) != NULL)
    {
        if(param == LcdSeg_Shine_OFF) temp = (LcdSeg_Shine_OFF & 0xf);
        else temp = (param / 100) % 10;
        Lcd_Number(temp,array3);
    }
    if((*array2) != NULL)
    {
        if(param == LcdSeg_Shine_OFF) temp = (LcdSeg_Shine_OFF & 0xf);
        else temp = (param / 10) % 10;
        Lcd_Number(temp,array2);
    }
    if((*array1) != NULL)
    {
        if(param == LcdSeg_Shine_OFF) temp = (LcdSeg_Shine_OFF & 0xf);
        else temp = param % 10;
        Lcd_Number(temp,array1);
    }
}

void Lcd_NumShow(UI_Number_E t,int32_t param)
{
    uint16_t temp = 0;

    if(t == UI_Timer_Hour)
    {
        if(param != LcdSeg_Shine_OFF) param *= 100;
        Lcd_Numbers(param,Timer_seg1,Timer_seg2,NULL,NULL);
    }
    else if(t == UI_Timer_Minute) Lcd_Numbers(param,NULL,NULL,Timer_seg3,Timer_seg4);

    if(UI_System.Grill_state != State_Grill_Misfire)
    {
        if(t == UI_RTD)
        {
            if(UI_System.Shine == Shine_All) param = 888;
            if(Temp_Param.oven_Err == Pt_Normal)
            {
                if(UI_System.State_Operation != UI_None)
                { //RTD setting界面
                    if(UI_System.Grill_Smoke == State_Grill_Smoke)
                    {
                        if(param == LcdSeg_Shine_OFF)
                        {
                            Lcd_Number(Nixie_NumEncoder_Space,TempAct_seg1);
                            Lcd_Number(Nixie_NumEncoder_Space,TempAct_seg2);
                            Lcd_Number(Nixie_NumEncoder_Space,TempAct_seg3);
                        }
                        else
                        {
                            Lcd_Number(Nixie_NumEncoder_S,TempAct_seg1);
                            Lcd_Number(Nixie_NumEncoder_U,TempAct_seg2);
                            Lcd_Number(Nixie_NumEncoder_P,TempAct_seg3);
                        }
                    }
                    else Lcd_Numbers(param,NULL,TempAct_seg1,TempAct_seg2,TempAct_seg3);
                } 
                else 
                {
                    if(param < 0)
                    {
                        Lcd_Number(Nixie_NumEncoder_Crossbar,TempAct_seg1);
                        param = -param;
                    }
                    else Lcd_Number(((param / 100) % 10),TempAct_seg1);
                    Lcd_Number(((param / 10) % 10),TempAct_seg2);
                    Lcd_Number((param % 10),TempAct_seg3);
                }
            } 
            else
            {
                if(Temp_Param.oven_Err <= Pt_HighTemp)
                {
                    Lcd_Number(Nixie_NumEncoder_E_Char,TempAct_seg1);
                    Lcd_Number(Nixie_NumEncoder_r,TempAct_seg2);
                    if(Temp_Param.oven_Err == Pt_Open) temp = 0;
                    else if(Temp_Param.oven_Err == Pt_Short) temp = 5;
                    else if(Temp_Param.oven_Err == Pt_HighTemp) temp = Nixie_NumEncoder_H;
                    else if(Temp_Param.oven_Err == Pt_LowTemp) temp = Nixie_NumEncoder_L;
                    Lcd_Number(temp,TempAct_seg3);
                }
                else
                {
                    // if(Temp_Param.oven_Err == Pt_EOr)
                    // {
                    //     Lcd_Number(Nixie_NumEncoder_E_Char,TempAct_seg1);
                    //     Lcd_Number(Nixie_NumEncoder_r,TempAct_seg2);
                    //     Lcd_Number(Nixie_NumEncoder_A,TempAct_seg3);
                    // }
                    // if(Temp_Param.oven_Err == Pt_EFn)
                    // {
                    //     Lcd_Number(Nixie_NumEncoder_E_Char,TempAct_seg1);
                    //     Lcd_Number(Nixie_NumEncoder_r,TempAct_seg2);
                    //     Lcd_Number(Nixie_NumEncoder_F,TempAct_seg3);
                    // }
                    // if(Temp_Param.oven_Err == Pt_Elr)
                    // {
                    //     Lcd_Number(Nixie_NumEncoder_E_Char,TempAct_seg1);
                    //     Lcd_Number(Nixie_NumEncoder_r,TempAct_seg2);
                    //     Lcd_Number(Nixie_NumEncoder_i,TempAct_seg3);
                    // }
                }
            }
        }
        else if(t == UI_RTDSettings)
        {
            if(UI_System.Shine == Shine_All) param = 888;
            if(UI_System.Grill_Smoke == State_Grill_Smoke)
            {
                Lcd_Number(Nixie_NumEncoder_S,TempSet_seg1);
                Lcd_Number(Nixie_NumEncoder_U,TempSet_seg2);
                Lcd_Number(Nixie_NumEncoder_P,TempSet_seg3);
            }
            else Lcd_Numbers(param,NULL,TempSet_seg1,TempSet_seg2,TempSet_seg3);
        }
        else if(t == UI_ProbeA)
        {
            if(UI_System.Shine == Shine_All) Lcd_Numbers(888,NULL,P1_seg1,P1_seg2,P1_seg3);
            else if(Temp_Param.probe1_Err == Pt_Normal)
            {
                if(param < 0)
                {
                    Lcd_Number(Nixie_NumEncoder_Crossbar,P1_seg1);
                    param = -param;
                }
                else Lcd_Number(((param / 100) % 10),P1_seg1);
                Lcd_Number(((param / 10) % 10),P1_seg2);
                Lcd_Number((param % 10),P1_seg3);
            } 
            else
            {
                Lcd_Number(Nixie_NumEncoder_Crossbar,P1_seg1);
                Lcd_Number(Nixie_NumEncoder_Crossbar,P1_seg2);
                Lcd_Number(Nixie_NumEncoder_Crossbar,P1_seg3);
            }
        }
        else if(t == UI_ProbeB)
        {
            if(UI_System.Shine == Shine_All) Lcd_Numbers(888,NULL,P2_seg1,P2_seg2,P2_seg3);
            else if(Temp_Param.probe2_Err == Pt_Normal)
            {
                if(param < 0)
                {
                    Lcd_Number(Nixie_NumEncoder_Crossbar,P2_seg1);
                    param = -param;
                }
                else Lcd_Number(((param / 100) % 10),P2_seg1);
                Lcd_Number(((param / 10) % 10),P2_seg2);
                Lcd_Number((param % 10),P2_seg3);
            }
            else
            {
                Lcd_Number(Nixie_NumEncoder_Crossbar,P2_seg1);
                Lcd_Number(Nixie_NumEncoder_Crossbar,P2_seg2);
                Lcd_Number(Nixie_NumEncoder_Crossbar,P2_seg3);
            }
        }
    }
    else if(UI_System.Timer1.Actual > 0)
    { //熄火状态
        if(t == UI_RTD)
        { //OFF
            Lcd_Number(0,TempAct_seg1);
            Lcd_Number(Nixie_NumEncoder_F,TempAct_seg2);
            Lcd_Number(Nixie_NumEncoder_F,TempAct_seg3);
        }
        else if(t == UI_RTDSettings) Lcd_Numbers(LcdSeg_Shine_OFF,NULL,TempSet_seg1,TempSet_seg2,TempSet_seg3);
        else if(t == UI_ProbeA) Lcd_Numbers(LcdSeg_Shine_OFF,NULL,P1_seg1,P1_seg2,P1_seg3);
        else if(t == UI_ProbeB) Lcd_Numbers(LcdSeg_Shine_OFF,NULL,P2_seg1,P2_seg2,P2_seg3);
    }
    else
    { //关机状态(不断电源,只关闭屏幕)
        if(t == UI_RTD) Lcd_Numbers(LcdSeg_Shine_OFF,NULL,TempAct_seg1,TempAct_seg2,TempAct_seg3);
        Lcd_Numbers(LcdSeg_Shine_OFF,Timer_seg1,Timer_seg2,NULL,NULL);
        Lcd_Numbers(LcdSeg_Shine_OFF,NULL,NULL,Timer_seg3,Timer_seg4);
        Lcd_Timer_Title(Char_OFF);
        Lcd_Wifi(Char_OFF);
        Lcd_Blue(Char_OFF);
        IO_Lcd_ON_OFF_L;
    }
}

void Lcd_NumShow_Shine(UI_Number_E t,uint32_t param)
{
    if(t == UI_System.State_Operation)
    {
        if(UI_System.Shine == Shine_OFF) param = LcdSeg_Shine_OFF;
    }
    Lcd_NumShow(t,param);
}
